﻿Option Infer On
Imports Core
Imports Feed.Base
Imports System.IO
Imports System.Xml
Imports System.Diagnostics
Imports Feed.Base.FeedBase
Namespace Database
    Public Enum ItemView
        Headline
        Summary
        Full
    End Enum
    ''TODO: Make this class inherit from FeedBase instead of duplicating most of its code
    Public Class SubscriptionManager
        Event RefreshAllFeedsComplete()
        Dim updThread As System.Threading.Thread
        Dim feedTitle, feedlink As String
        Dim elementNames As Object()
        Dim FeedParse As FeedBase = New FeedBase()
        Public Sub New(ByVal filename As String)
            LoadList(filename)
            For Each item As FeedDBItem In [Global].Feeds
                LoadFeedItems(item.Guid)
            Next
        End Sub
#Region " List Loading"
        Private Sub LoadList(ByVal filename As String)
            Dim xtr = New XmlTextReader(New IO.FileStream(filename, FileMode.Open))
            elementNames = ReturnNameTable(xtr.NameTable)
            ProcessList(xtr, elementNames)
        End Sub
        Private Function IsItemNothing(ByVal fi As FeedDBItem) As Boolean ' And Not fi.Content = "" And Not fi.pubDate.ToString = "#12:00:00 AM#"
            If Not fi.Guid = "" Then Return False 'And Not fi.Link = "" Then Return False
            Return True
        End Function
        Private Sub ProcessList(ByRef reader As XmlReader, ByVal elementNames As Object())
            Dim name = reader.LocalName
            Do Until reader.EOF = True
                reader.Read()
                If reader.NodeType <> XmlNodeType.Element Then Continue Do
                name = reader.LocalName
                If name.Equals(elementNames(title).ToString) Then feedTitle = reader.ReadElementString("title")
                If name.Equals(elementNames(link).ToString) Then feedlink = reader.ReadElementString("link")
                If name.Equals(elementNames(description).ToString) Then Debug.Print(reader.ReadElementString("description"))
                If name.Equals(elementNames(item).ToString) Then
                    Dim item As FeedDBItem = ProcessItem(reader, Formats.RSS, elementNames)
                    If IsItemNothing(item) = False Then
                        item.FeedName = feedTitle
                        If IsDupe(item.Title) = False Then
                            [Global].Feeds.Add(item)
                            reader.Skip()
                        Else
                            reader.Skip()
                        End If
                    End If
                End If
            Loop
            reader.Close()
        End Sub
        Private Function IsDupe(ByVal itemTitle As String)
            For Each item As FeedDBItem In [Global].Feeds
                If item.FeedName = itemTitle Then Return True
            Next
            Return False
        End Function
        Protected Function ProcessItem(ByRef reader As XmlReader, ByVal format As Formats, ByVal elementNames As Object()) As IBaseItem
            Dim titlevar As String = ""
            Dim URLvar As String = ""
            Dim authorvar As String = ""
            Dim bodyvar As String = ""
            Dim subjvar As String = ""
            Dim guidvar As String = ""
            Dim pubDate As Date = Nothing
            Dim category As String = Nothing
            Dim feedItem As FeedDBItem = Nothing
            Dim endOfNode As Boolean
            Dim name As Object
            Dim i As VersionNotFoundException
            While (endOfNode OrElse reader.Read())
                If reader.NodeType <> XmlNodeType.Element Then Continue While
                name = reader.LocalName
                endOfNode = False
                If reader.NodeType = XmlNodeType.Whitespace Then reader.MoveToContent()
                If IsNothing(feedItem) Then feedItem = New FeedDBItem
                If reader.NodeType = XmlNodeType.EndElement Then
                    If name.Equals(elementNames(item).ToString) Then endOfNode = True
                End If
                If name.Equals(elementNames(title).ToString) Then titlevar = reader.ReadElementString("title")
                If name.Equals(elementNames(description).ToString) Then bodyvar = reader.ReadElementString("description")
                If name.Equals(elementNames(link).ToString) Then URLvar = reader.ReadElementString("link")
                If name.Equals(elementNames(category).ToString) Then category = reader.ReadElementString("category")
                If name.Equals(elementNames(guid).ToString) Then
                    guidvar = reader.ReadElementString("guid")
                End If
                feedItem.Link = URLvar
                feedItem.Title = titlevar
                feedItem.Guid = guidvar
                feedItem.Category = category
                If IsItemNothing(feedItem) = False Then
                    Return feedItem
                End If
            End While
            Return Nothing
        End Function
#End Region
#Region " Feed List Operations"
        Public Sub LoadFeedItems(ByVal guid As String)
            FeedParse.GetFeedXml(guid)
        End Sub
        Public Sub StartRefreshAllFeeds()
            updThread = New System.Threading.Thread(AddressOf RefreshAllFeeds)
            updThread.Start()
        End Sub
        Private Sub RefreshAllFeeds()
            For Each item As FeedDBItem In [Global].Feeds
                FeedParse.GetFeedXml(New Uri(item.Link), False, item)
            Next
            RaiseEvent RefreshAllFeedsComplete()
        End Sub
        ''TODO: Save the feeds list
        Public Sub SaveFeedList()
            Throw New NotImplementedException
        End Sub
#End Region
    End Class
End Namespace